home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / Snippets / Printing / Offscreen region maskRgn / Template.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-24  |  4.2 KB  |  195 lines  |  [TEXT/KAHL]

  1. /**------------------------------------------------------------------------------------- 
  2.  --
  3.  --  A very simple printing loop template   
  4.  --
  5.  **-----------------------------------------------------------------------------------**/
  6.  
  7. #include "Printing.h"
  8. #include <QDOffscreen.h>
  9.  
  10.  
  11. void PrintStuff (void);
  12. void DrawStuff (Rect theWorld);
  13. void PrepareOffScreens(void);
  14. void KillOffscreens(void);
  15.  
  16. /*------ main ----------------------------------------------------------------------------*/
  17.  
  18. CGrafPtr offscreen1, offscreen2;
  19. RgnHandle textRegion;
  20. Rect boundsRect;
  21.  
  22. main()
  23.  
  24. {
  25.  
  26.     GrafPort myPort;
  27.  
  28.     InitGraf((Ptr) &thePort);
  29.     OpenPort(&myPort);
  30.     InitFonts();
  31.     InitWindows();
  32.     InitMenus();
  33.     TEInit();
  34.     InitDialogs(nil);
  35.     InitCursor();
  36.  
  37.     PrepareOffScreens();
  38.     PrintStuff ();
  39.     KillOffscreens();
  40.     
  41. } /* main */
  42.  
  43.  
  44. /*------ PrepareOffScreens ---------------------------------------------------------------*/
  45.  
  46. void PrepareOffScreens()
  47.  
  48. {
  49.  
  50.     OSErr myErr;
  51.     CGrafPtr oldCPort;
  52.     GDHandle oldGD;
  53.     short fontNum;
  54.     PixPatHandle ourPixPat;
  55.     
  56.     SetRect(&boundsRect,0,0,700,700);
  57.     GetGWorld(&oldCPort,&oldGD);
  58.     
  59.     /* create offscreen #1 */
  60.     
  61.     myErr = NewGWorld(&offscreen1, 1, &boundsRect, NULL, NULL, 0);
  62.     if (myErr != noErr) {
  63.         DebugStr("\pCouldn't create GWorld #1");
  64.         ExitToShell();
  65.     }
  66.     
  67.     SetGWorld(offscreen1,NULL);
  68.     ClipRect(&boundsRect);
  69.     
  70.     ForeColor(blackColor);
  71.     EraseRect(&boundsRect);
  72.     GetFNum("\pTimes",&fontNum);
  73.     TextFont(fontNum);
  74.     TextFace(bold);
  75.     TextSize(60);
  76.     MoveTo(10,100);
  77.     DrawString("\pHere’s our string test");
  78.     
  79.     /* OK, offscreen #1 now has our string in it.  Let's make a region from it. */
  80.     
  81.     textRegion = NewRgn();
  82.     myErr = BitMapToRegion(textRegion,&((GrafPtr)offscreen1)->portBits);
  83.     if ((myErr != noErr) || ((**textRegion ).rgnSize == 10)) {
  84.         DebugStr("\pCouldn't convert text to region");
  85.         ExitToShell();
  86.     }
  87.     
  88.     /* create offscreen #2 */
  89.     
  90.     myErr = NewGWorld(&offscreen2, 4, &boundsRect, NULL, NULL, 0);
  91.     if (myErr != noErr) {
  92.         DebugStr("\pCouldn't create GWorld #2");
  93.         ExitToShell();
  94.     }
  95.     
  96.     /* Fill it with a color pattern so we can see what's going on */
  97.     
  98.     SetGWorld(offscreen2,NULL);
  99.     ClipRect(&boundsRect);
  100.     
  101.     ourPixPat = GetPixPat(16);
  102.     FillCRect(&boundsRect,ourPixPat);
  103.     DisposePixPat(ourPixPat);
  104.     
  105.     SetGWorld(oldCPort,oldGD);
  106.     
  107. }
  108.  
  109. /*------ DrawStuff -----------------------------------------------------------------------*/
  110.  
  111. void DrawStuff (theRect)
  112.  
  113. Rect         theRect;
  114.  
  115. {
  116.     GrafPtr    currentPort;
  117.     PixMapHandle offscreenPix;
  118.  
  119.     /* Here's where we use CopyBits to copy from offscreen2 through the mask region */
  120.     
  121.     GetPort(¤tPort);
  122.     offscreenPix = GetGWorldPixMap(offscreen2);    
  123.     LockPixels(offscreenPix);
  124.     DebugStr("\pJust about to CopyBits...");
  125.     CopyBits((BitMap*)*offscreenPix, 
  126.              &(currentPort)->portBits,
  127.              &boundsRect, &boundsRect, srcCopy, textRegion);
  128.     UnlockPixels(offscreenPix);
  129.  
  130.  
  131. }  /* DrawStuff */
  132.  
  133.  
  134.  
  135. /*------ PrintStuff ----------------------------------------------------------------------*/
  136.  
  137. void PrintStuff ()
  138. {
  139.     GrafPtr        oldPort;
  140.     THPrint        thePrRecHdl;
  141.     TPPrPort    thePrPort;
  142.     TPrStatus    theStatus;
  143.  
  144.     GetPort(&oldPort);
  145.     
  146.     thePrRecHdl = (THPrint)  NewHandle (sizeof (TPrint));
  147.     
  148.     PrOpen();
  149.  
  150.         if (PrError() == noErr)
  151.          {
  152.             PrintDefault(thePrRecHdl);
  153.             
  154.                PrValidate(thePrRecHdl);
  155.              
  156.             if (! PrStlDialog(thePrRecHdl)) 
  157.               ExitToShell();
  158.               
  159.             if (! PrJobDialog(thePrRecHdl)) 
  160.               ExitToShell();
  161.               
  162.             thePrPort = PrOpenDoc(thePrRecHdl, nil, nil);
  163.         
  164.             if (PrError() == noErr) 
  165.              {
  166.                 PrOpenPage(thePrPort, nil);
  167.                 if (PrError() == noErr) 
  168.                  {
  169.                     /********* Print from here ****************/
  170.                     
  171.                     DrawStuff ((**thePrRecHdl).prInfo.rPage);
  172.                  }
  173.              }
  174.             PrClosePage(thePrPort);
  175.         }
  176.         PrCloseDoc(thePrPort);
  177.     
  178.     if ((((TPPrint)*thePrRecHdl)->prJob.bJDocLoop == bSpoolLoop) && (PrError() == noErr))
  179.         PrPicFile(thePrRecHdl, nil, nil, nil, &theStatus);                
  180.  
  181.     PrClose();
  182.  
  183.     SetPort(oldPort);
  184. }  /* PrintStuff */
  185.  
  186. /*------ KillOffscreens  ------------------------------------------------------------------*/
  187.  
  188. void KillOffscreens()
  189.  
  190. {
  191.     DisposeGWorld(offscreen1);
  192.     DisposeGWorld(offscreen2);
  193.     DisposeRgn(textRegion);
  194.  
  195. }